package com.shycloud.mido.common.security.component;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.security.exception.ShyAuth2Exception;
import lombok.SneakyThrows;
import org.springframework.security.authentication.LockedException;

/**
 * @author npc
 * @date 2018/11/16
 * <p>
 * OAuth2 异常格式化
 */
public class ShyAuth2ExceptionSerializer extends StdSerializer<ShyAuth2Exception> {
	public ShyAuth2ExceptionSerializer() {
		super(ShyAuth2Exception.class);
	}

	@Override
	@SneakyThrows
	public void serialize(ShyAuth2Exception value, JsonGenerator gen, SerializerProvider provider) {

		gen.writeStartObject();
		gen.writeObjectField("code", CommonConstants.FAIL);
		// 目前只能这么改，由于时间问题不好研究源码
		gen.writeStringField("msg", "用户帐号已被锁定".equals(value.getMessage()) ? "该账号已停止服务" : value.getMessage());
		gen.writeStringField("data", value.getErrorCode());
		gen.writeEndObject();
	}
}
